package com.yz.platform.common.message;

import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 异步响应消息
 * Created by lin on 2017/4/17.
 */
public class RespMsg<T> implements Serializable {

    /**
     * 响应编码
     *
     * @see org.springframework.http.HttpStatus
     */
    private Integer code;

    /**
     * 自定义消息内容
     */
    private String msg;

    /**
     * 消息数据
     */
    private T data;


    public RespMsg() {
    }

    public RespMsg(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public RespMsg(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 操作成功
     *
     * @return
     */
    public static <T> RespMsg<T> success() {
        return new RespMsg<T>(HttpStatus.OK.value(), "操作成功");
    }

    /**
     * 操作成功
     *
     * @return
     */
    public static RespMsg<String> success(String msg) {
        return new RespMsg<String>(HttpStatus.OK.value(), msg);
    }

    /**
     * 操作失败
     *
     * @return
     */
    public static <T> RespMsg<T> fail() {
        return new RespMsg<T>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作失败");
    }


    /**
     * 操作失败
     *
     * @return
     */
    public static RespMsg<String> fail(String msg) {
        return new RespMsg<String>(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    @Override
    public String toString() {
        return "RespMsg{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}